package com.VisualDisp.QuadCalculateProject.VisualDisplay;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;

public class MainDow {

	protected Shell shlQuadraticEquationCalculator;
	private Text text;
	private Text text_1;
	private Text text_2;
	private Text text_3;
	private Text text_4;
	private Text text_5;
	private Text text_6;
	private Button plotBtn;
	
	private MainDowShell myShell;
	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			MainDow window = new MainDow();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shlQuadraticEquationCalculator.open();
		shlQuadraticEquationCalculator.layout();
		while (!shlQuadraticEquationCalculator.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}
	
	private void CheckKey(KeyEvent e, Text text)
	{
		char temp = e.character;
		if((temp < '0' || temp > '9') && (temp != '-' ))
			text.setText("0");
	}
	
	private boolean condMatch()
	{
		String t = text_6.getText();
		return (!t.equalsIgnoreCase("True Expression") && !t.equalsIgnoreCase("False Experssion"));
	}
	private void Clear()
	{
		text.setText("0");
		text_1.setText("0");
		text_2.setText("0");
		text_3.setText("");
		text_4.setText("");
		text_5.setText("");
		text_6.setText("");
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shlQuadraticEquationCalculator = new Shell();
		shlQuadraticEquationCalculator.setImage(SWTResourceManager.getImage("C:\\Users\\avishai\\workspace\\QuadEquaCalc\\Res\\images.jpg"));
		shlQuadraticEquationCalculator.setText("Quadratic Equation Calculator");
		shlQuadraticEquationCalculator.setSize(527, 380);
		
		text = new Text(shlQuadraticEquationCalculator, SWT.BORDER);
		text.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				CheckKey(e,text);
			}
		});
		text.setText("0");
		text.setBounds(45, 83, 33, 21);
		
		text_1 = new Text(shlQuadraticEquationCalculator, SWT.BORDER);
		text_1.addKeyListener(new KeyAdapter(){
			
			public void keyReleased(KeyEvent e)
			{
				CheckKey(e,text_1);
			}
		});
		text_1.setText("0");
		text_1.setBounds(45, 110, 33, 21);
		
		text_2 = new Text(shlQuadraticEquationCalculator, SWT.BORDER);
		text_2.addKeyListener(new KeyAdapter(){
			
			public void keyReleased(KeyEvent e)
			{
				CheckKey(e,text_2);
			}
		});
		text_2.setText("0");
		text_2.setBounds(45, 137, 33, 21);
		
		CLabel lblA = new CLabel(shlQuadraticEquationCalculator, SWT.NONE);
		lblA.setBounds(23, 83, 17, 21);
		lblA.setText("A:");
		
		CLabel lblB = new CLabel(shlQuadraticEquationCalculator, SWT.NONE);
		lblB.setText("B:");
		lblB.setBounds(23, 110, 17, 21);
		
		CLabel lblC = new CLabel(shlQuadraticEquationCalculator, SWT.NONE);
		lblC.setText("C:");
		lblC.setBounds(23, 137, 17, 21);
		
		text_3 = new Text(shlQuadraticEquationCalculator, SWT.BORDER);
		text_3.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_HIGHLIGHT_SHADOW));
		text_3.setEditable(false);
		text_3.setBounds(244, 110, 135, 21);
		
		CLabel lblEquation = new CLabel(shlQuadraticEquationCalculator, SWT.NONE);
		lblEquation.setBounds(171, 110, 61, 21);
		lblEquation.setText("Equation:");
		
		CLabel lblX_1 = new CLabel(shlQuadraticEquationCalculator, SWT.NONE);
		lblX_1.setText("X1:");
		lblX_1.setBounds(183, 185, 22, 21);
		
		text_4 = new Text(shlQuadraticEquationCalculator, SWT.BORDER);
		text_4.setEditable(false);
		text_4.setBounds(210, 185, 33, 21);
		
		CLabel lblX = new CLabel(shlQuadraticEquationCalculator, SWT.NONE);
		lblX.setText("X2:");
		lblX.setBounds(259, 185, 22, 21);
		
		text_5 = new Text(shlQuadraticEquationCalculator, SWT.BORDER);
		text_5.setEditable(false);
		text_5.setBounds(286, 185, 33, 21);
		
		Button btnCalculate = new Button(shlQuadraticEquationCalculator, SWT.NONE);
		btnCalculate.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				double temp1,temp2,temp3;
				try
				{
					temp1 = Double.parseDouble(text.getText().trim());
					temp2 = Double.parseDouble(text_1.getText().trim());
					temp3 = Double.parseDouble(text_2.getText().trim());
					myShell  = new MainDowShell(temp1,temp2,temp3, text_3, text_6, text_4, text_5);
					myShell.doWork();
					if(condMatch())
						plotBtn.setEnabled(true);
				}
				catch(Exception ex)
				{
					ex.printStackTrace();
					text_6.setText("Not a number!!!");
					plotBtn.setEnabled(false);
				}
				
			}
		});
		btnCalculate.setBounds(117, 254, 75, 25);
		btnCalculate.setText("Calculate");
		
		Button btnClear = new Button(shlQuadraticEquationCalculator, SWT.NONE);
		btnClear.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Clear();
			}
		});
		btnClear.setText("Clear");
		btnClear.setBounds(217, 254, 75, 25);
		
		plotBtn = new Button(shlQuadraticEquationCalculator, SWT.NONE);
		plotBtn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				new Plot(myShell.getExpression()).setVisible(true);
			}
		});
		plotBtn.setToolTipText("Graph");
		plotBtn.setBounds(322, 254, 82, 25);
		plotBtn.setText("GraphDisplay");
		
		CLabel lblType = new CLabel(shlQuadraticEquationCalculator, SWT.NONE);
		lblType.setText("Type:");
		lblType.setBounds(171, 83, 61, 21);
		
		text_6 = new Text(shlQuadraticEquationCalculator, SWT.BORDER);
		text_6.setEditable(false);
		text_6.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_HIGHLIGHT_SHADOW));
		text_6.setBounds(244, 83, 135, 21);

	}
}
